home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / CHESS.PAK / DISPLAY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  13.7 KB  |  573 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl/pch.h>
  5. #include <owl/defs.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <bwcc.h>
  9. #include "wcdefs.h"
  10. #include "wchess.h"
  11. #include "info.h"
  12. #include "externs.h"
  13.  
  14.  
  15. //
  16. //  Global variables
  17. //
  18. BOARDIDTYPE Display[0x78];
  19. char *PieceLetter = " KQRBNP";
  20. char buf[280];   //  general string buffer, used in several modules
  21.  
  22. //
  23. //  static global variables
  24. //
  25. static TRect BoardRect;
  26.  
  27. //
  28. //  get handle to bitmap of current piece
  29. //
  30. HBITMAP
  31. GetBitmapHandle(PIECETYPE piece, COLORTYPE pcolor)
  32. {
  33.   if (!piece)
  34.     return 0;
  35.   return PieceBmpArray[piece - 1][pcolor];
  36. }
  37.  
  38.  
  39. //
  40. //  Clear all information from Info window
  41. //
  42. void
  43. ClearInfoWindow()
  44. {
  45.   TInfo->Reset();
  46. }
  47.  
  48. //
  49. //  Prints current color to play
  50. //
  51. void
  52. ColorToPlay(COLORTYPE color)
  53. {
  54.   TInfo->SetTurnText(color == white ? "White" : "Black");
  55. }
  56.  
  57. void
  58. Message(char* str)
  59. {
  60.   TInfo->SetMessageText(str);
  61. }
  62.  
  63. void
  64. Error(char* str)
  65. {
  66.   if (SoundOn)
  67.     MessageBeep(0);
  68.   strcpy(buf, str);
  69.   SendMessage(hWndMain, WM_COMMAND, EM_ERROR, 0L);
  70. }
  71.  
  72. void
  73. Warning(char* str)
  74. {
  75.   if (SoundOn)
  76.     MessageBeep(0);
  77.   Message(str);
  78. }
  79.  
  80. //
  81. //  convert a move to a string
  82. //
  83. char*
  84. MoveStr(MOVETYPE* move)
  85. {
  86.   static char str[7];
  87.  
  88.   strcpy(str, "   ");
  89.   if (move->movpiece != empty) {
  90.     if (move->spe && move->movpiece == king) { //  castling
  91.       if (move->new1 > move->old)
  92.         strcpy(str, "O-O  ");
  93.       else
  94.         strcpy(str, "O-O-O ");
  95.  
  96.     } else {
  97.       str[0] = PieceLetter[move->movpiece];
  98.       str[1] = char('a' + move->old % 16);
  99.       str[2] = char('1' + move->old / 16);
  100.       str[3] = move->content == empty ? '-' : 'x';
  101.       str[4] = char('a' + move->new1 % 16);
  102.       str[5] = char('1' + move->new1 / 16);
  103.     }
  104.   }
  105.   return str;
  106. }
  107.  
  108.  
  109. void
  110. PrintMove(int moveno, COLORTYPE programcolor, MOVETYPE* move, double time)
  111. {
  112.   int minutes = (int)(time / 60.0);
  113.  
  114.   sprintf(buf, "%2.2d:%#04.1f %3.3d. %s", minutes, time - minutes * 60.0, moveno / 2 + 1, MoveStr(move));
  115.   if (programcolor == white)
  116.     TInfo->SetWhiteInfoText(buf);
  117.   else
  118.     TInfo->SetBlackInfoText(buf);
  119. }
  120.  
  121. //
  122. //  draw 3d type frame
  123. //
  124. void
  125. DrawFrame(HDC hDC, TRect& rect, BOOL drawBackground)
  126. {
  127.   int x1 = rect.left;
  128.   int x2 = rect.right;
  129.   int y1 = rect.top;
  130.   int y2 = rect.bottom;
  131.  
  132.   //
  133.   // Draw the outline of the frame & fill to erase if requested.
  134.   //
  135.   HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC,
  136.       GetStockObject(drawBackground ? LTGRAY_BRUSH : NULL_BRUSH));
  137.   HPEN hOldPen = (HPEN)SelectObject(hDC, GetStockObject(WHITE_PEN));
  138.   Rectangle(hDC, x1, y1, x2, y2);
  139.  
  140.   //
  141.   // Draw light grey rectangle that is the high part of the frame
  142.   //
  143.   HPEN hPen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
  144.   SelectObject(hDC, hPen);
  145.   Rectangle(hDC, x1+1, y1+1, x2-1, y2-1);
  146.   DeleteObject(SelectObject(hDC, GetStockObject(WHITE_PEN)));
  147.  
  148.   //
  149.   // Draw highlight for right & bottom of frame
  150.   //
  151.   TPoint points[3];
  152.   points[0].x = x1 + 2;
  153.   points[1].y = points[0].y = y2 - 3;
  154.   points[2].x = points[1].x = x2 - 3;
  155.   points[2].y = y1 + 2;
  156.   Polyline(hDC, points, 3);
  157.  
  158.   //
  159.   // Draw shadow for right & bottom of frame
  160.   //
  161.   hPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  162.   SelectObject(hDC, hPen);
  163.   points[0].x = x1;
  164.   points[1].y = points[0].y = y2-1;
  165.   points[2].x = points[1].x = x2-1;
  166.   points[2].y = y1;
  167.   Polyline(hDC, points, 3);
  168.   SetPixel(hDC, x2-1, y2-1, RGB(128, 128, 128));
  169.  
  170.   //
  171.   // Draw shadow for left & top of frame
  172.   //
  173.   points[1].x = points[0].x = x1 + 2;
  174.   points[0].y = y2 - 3;
  175.   points[2].y = points[1].y = y1 + 2;
  176.   points[2].x = x2 - 3;
  177.   Polyline(hDC, points, 3);
  178.   SelectObject(hDC, hOldBrush);
  179.   DeleteObject(SelectObject(hDC, hOldPen));
  180.  
  181.   //
  182.   // Touch-up bevel corners where shadows meet highlights
  183.   //
  184.   SetPixel(hDC, x2-1, y1,   RGB(192, 192, 192));
  185.   SetPixel(hDC, x2-3, y1+2, RGB(192, 192, 192));
  186.   SetPixel(hDC, x1,   y2-1, RGB(192, 192, 192));
  187.   SetPixel(hDC, x1+2, y2-3, RGB(192, 192, 192));
  188. }
  189.  
  190. //
  191. //  Display the current level indicator
  192. //
  193. void
  194. PrintCurLevel()
  195. {
  196.   extern BOOL MultiMove;
  197.  
  198.   if (MultiMove)
  199.     strcpy(buf, "Two Player");
  200.   else {
  201.     switch (Level) {
  202.       case normal:
  203.         sprintf(buf, "%1.0f sec / move", AverageTime);
  204.         break;
  205.       case fullgametime:
  206.         sprintf(buf, "%2.2f min / game", AverageTime);
  207.         break;
  208.       case easygame:
  209.         strcpy(buf, "Easy");
  210.         break;
  211.       case infinite :
  212.         strcpy(buf, "Infinte");
  213.         break;
  214.       case plysearch :
  215.         sprintf(buf, "Ply-Depth = %d", MaxLevel);
  216.         break;
  217.       case matesearch:
  218.         strcpy(buf, "MateSearch");
  219.         break;
  220.       case matching :
  221.         strcpy(buf, "Match users time");
  222.         break;
  223.     }
  224.   }
  225.   TInfo->SetLevelText(buf);
  226. }
  227.  
  228. TPoint
  229. GetSquareXY(SQUARETYPE square)
  230. {
  231.   if (Turned)
  232.     square ^= 0x77;
  233.   return TPoint((square % 8) * SQUARE_SIZE + BORDERSIZE + MYFRAMESIZE,
  234.                 (7 - square / 16) * SQUARE_SIZE + BORDERSIZE + MYFRAMESIZE);
  235. }
  236.  
  237. void
  238. ClearSquare(SQUARETYPE square)
  239. {
  240.   TPoint p = GetSquareXY(square);
  241.   HDC hDC = GetDC(hWndMain);
  242.  
  243.   HANDLE hOldBrush;
  244.   if ((square % 8 + square /16) % 2 == 1)
  245.     hOldBrush = SelectObject(hDC, hWhiteBrush);
  246.   else
  247.     hOldBrush = SelectObject(hDC, hBlackBrush);
  248.   PatBlt(hDC, p.x, p.y, SQUARE_SIZE, SQUARE_SIZE, PATCOPY);
  249.   SelectObject(hDC, hOldBrush);
  250.   ReleaseDC(hWndMain, hDC);
  251. }
  252.  
  253. void
  254. ClearDisplay()
  255. {
  256.   ClearInfoWindow();
  257.   for (SQUARETYPE sq = 0; sq <= 0x77; sq++)
  258.     Display[sq].piece = empty;
  259. }
  260.  
  261. //
  262. //  Draw the board on the screen
  263. //
  264. void
  265. DrawBoard()
  266. {
  267.   unsigned char no;
  268.   HDC hDC;
  269.   const SQUARETYPE printno[64] = {
  270.     0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  271.     0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x67,
  272.     0x57, 0x47, 0x37, 0x27, 0x17, 0x07, 0x06, 0x05,
  273.     0x04, 0x03, 0x02, 0x01, 0x11, 0x21, 0x31, 0x41,
  274.     0x51, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x56,
  275.     0x46, 0x36, 0x26, 0x16, 0x15, 0x14, 0x13, 0x12,
  276.     0x22, 0x32, 0x42, 0x52, 0x53, 0x54, 0x55, 0x45,
  277.     0x35, 0x25, 0x24, 0x23, 0x33, 0x43, 0x44, 0x34
  278.   };
  279.  
  280.   BoardRect.left = BoardRect.top = BORDERSIZE;
  281.   BoardRect.right = BoardRect.bottom = BORDERSIZE + 2*MYFRAMESIZE + 8*SQUARE_SIZE;
  282.  
  283.   hDC = GetDC(hWndMain);
  284.   DrawFrame(hDC, BoardRect);
  285.   ReleaseDC(hWndMain, hDC);
  286.   for (no = 0; no < 64; no++)
  287.     ClearSquare(printno[no]);
  288. }
  289.  
  290.  
  291. void
  292. PrintPiece(SQUARETYPE square, PIECETYPE piece, COLORTYPE color, DWORD Rop)
  293. {
  294.   if (piece == empty)
  295.    return;
  296.  
  297.   HBITMAP hBitmap = PieceBmpArray[piece-1][color];
  298.   HBITMAP hMaskBmp = MaskArray[piece-1];
  299.  
  300.   HDC hDC = GetDC(hWndMain);
  301.   HDC hMemoryDC = CreateCompatibleDC(hDC);
  302.  
  303.   BITMAP bitmap;
  304.   GetObject(hBitmap, sizeof(BITMAP), &bitmap);
  305.   HANDLE hOldBmp = SelectObject(hMemoryDC, hMaskBmp);
  306.   TPoint p = GetSquareXY(square);
  307.  
  308.   BitBlt(hDC, p.x, p.y, bitmap.bmWidth, bitmap.bmHeight, hMemoryDC, 0, 0, SRCAND);
  309.   SelectObject(hMemoryDC, hBitmap);
  310.   BitBlt(hDC, p.x, p.y, bitmap.bmWidth, bitmap.bmHeight, hMemoryDC, 0, 0, Rop);
  311.   SelectObject(hMemoryDC, hOldBmp);
  312.   DeleteDC(hMemoryDC);
  313.   ReleaseDC(hWndMain, hDC);
  314. }
  315.  
  316. void
  317. InitDisplay()
  318. {
  319.   for (SQUARETYPE square = 0; square <= 0x77; square++)
  320.     if (!(square & 0x88))
  321.       if (Board[square].piece != Display[square].piece ||
  322.           Board[square].piece != empty &&
  323.           Board[square].color != Display[square].color) {
  324.         Display[square].piece = Board[square].piece;
  325.         Display[square].color = Board[square].color;
  326.       }
  327. }
  328.  
  329. //
  330. //  Draw a box in the square of an attacked piece.
  331. //  Square is black if Defended is TRUE, else it is red.
  332. //
  333. static void
  334. FrameSquare(SQUARETYPE square, BOOL Defended)
  335. {
  336.   HDC hDC;
  337.   hDC = GetDC(hWndMain);
  338.  
  339.   TPoint p = GetSquareXY(square);
  340.   HANDLE hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH));
  341.   HANDLE hOldPen = SelectObject(hDC,
  342.     Defended ? GetStockObject(BLACK_PEN) : CreatePen(PS_SOLID,1,RGB(192, 0, 0)));
  343.  
  344.   Rectangle(hDC, p.x+1, p.y+1, p.x+SQUARE_SIZE-1, p.y+SQUARE_SIZE-1);
  345.   SelectObject(hDC, hOldBrush);
  346.   SelectObject(hDC, hOldPen);
  347.   ReleaseDC(hWndMain, hDC);
  348. }
  349.  
  350. void
  351. HideAttacks()
  352. {
  353.   for (SQUARETYPE square = 0; square <= 0x77; square++)
  354.     if (!(square & 0x88)) {
  355.       if (Board[square].attacked) {
  356.         Board[square].attacked = FALSE;
  357.         ClearSquare(square);
  358.         PrintPiece(square, Board[square].piece, Board[square].color, SRCINVERT);
  359.       }
  360.     }
  361. }
  362.  
  363. void
  364. ShowAttacks()
  365. {
  366.   for (SQUARETYPE square = 0; square <= 0x77; square++)
  367.     if (!(square & 0x88)) {
  368.       if (Attacks(ComputerColor, square) && Board[square].color != ComputerColor && Board[square].piece != empty) {
  369.         Board[square].attacked = TRUE;
  370.         if (Attacks((COLORTYPE)!ComputerColor, square))
  371.           FrameSquare(square, TRUE);
  372.         else
  373.           FrameSquare(square, FALSE);
  374.  
  375.       } else if (Board[square].attacked) {
  376.         Board[square].attacked = FALSE;
  377.         ClearSquare(square);
  378.         PrintPiece(square, Board[square].piece, Board[square].color, SRCINVERT);
  379.       }
  380.     }
  381. }
  382.  
  383. void
  384. UpdateBoard()
  385. {
  386.   for (SQUARETYPE square = 0; square <= 0x77; square++)
  387.     if (!(square & 0x88))
  388.       if (Board[square].piece != Display[square].piece ||
  389.           Board[square].piece != empty &&
  390.           Board[square].color != Display[square].color) {
  391.         if (Display[square].piece != empty)
  392.           ClearSquare(square);
  393.         Display[square].piece = Board[square].piece;
  394.         Display[square].color = Board[square].color;
  395.         if (Board[square].piece != empty)
  396.           PrintPiece(square, Board[square].piece,Board[square].color, SRCINVERT);
  397.       }
  398.   if (Level == easygame && !Editing)
  399.     ShowAttacks();
  400. }
  401.  
  402. static void DrawAlphaNum();
  403.  
  404. void
  405. PrintBoard()
  406. {
  407.   DrawBoard();
  408.   for (SQUARETYPE square = 0; square <= 0x77; square++)
  409.     if (!(square & 0x88)) {
  410.       if (Display[square].piece != empty)
  411.         PrintPiece(square, Display[square].piece, Display[square].color, SRCINVERT);
  412.     }
  413.   DrawAlphaNum();
  414.   if (Level == easygame && !Editing)
  415.     ShowAttacks();
  416. }
  417.  
  418. //
  419. //  find a square with a given point and determine whether it
  420. //  contains a player of the given color
  421. //
  422. SQUARETYPE
  423. GetValidSquare(TPoint p, COLORTYPE player, BOOL CheckPiece)
  424. {
  425.   for (SQUARETYPE square = 0; square <= 0x77; square++) {
  426.     if (!(square & 0x88)) {
  427.       TPoint point = GetSquareXY(square);
  428.       TRect  sqrect(point, TSize(SQUARE_SIZE, SQUARE_SIZE));
  429.       if (sqrect.Contains(p)) {
  430.         if ((Display[square].color == player && Display[square].piece != empty) || !CheckPiece)
  431.           return square;
  432.       }
  433.     }
  434.   }
  435.   return -1;
  436. }
  437.  
  438. void
  439. DrawNormalBitmap(SQUARETYPE square)
  440. {
  441.   ClearSquare(square);
  442.   PrintPiece(square, Display[square].piece, Display[square].color, SRCINVERT);
  443. }
  444.  
  445. void
  446. DrawInvertedBitmap(SQUARETYPE square)
  447. {
  448.   PrintPiece(square, Display[square].piece, Display[square].color, NOTSRCERASE);
  449. }
  450.  
  451. void
  452. OpeningLibMsg()
  453. {
  454.   TInfo->SetMessageText("Using opening library");
  455. }
  456.  
  457. void
  458. PrintNodes(NODEVAL* nodes, double time)
  459. {
  460.   double nodereal = nodes->nodebase * MAXINT + nodes->nodeoffset;
  461.   char buf[80];
  462.   if (time) {
  463.     sprintf(buf, "%7.1f", nodereal/time);
  464.     TInfo->SetSecondsText(buf);
  465.   }
  466.   sprintf(buf, "%7.0f ", nodereal);
  467.   TInfo->SetNodeText(buf);
  468. }
  469.  
  470. //
  471. //  Print bestline on screen
  472. //
  473. void
  474. PrintBestMove(MOVETYPE *mainline, MAXTYPE mainevalu)
  475. {
  476.   if (ShowBestLine == FALSE)
  477.     return;
  478.  
  479.   *buf = 0;
  480.   DEPTHTYPE dep = 0;
  481.   while (dep < 7 && mainline[dep].movpiece != empty) {
  482.    strcat(buf, MoveStr(&mainline[dep++]));
  483.    strcat(buf, " ");
  484.   }
  485.   TInfo->SetBestLineText(buf);
  486.   sprintf(buf, "%7.2f", mainevalu/256.0);
  487.   TInfo->SetValueText(buf);
  488. }
  489.  
  490. void
  491. ClearBestLine()
  492. {
  493.   TInfo->SetBestLineText("");
  494. }
  495.  
  496. void
  497. ClearMessage()
  498. {
  499.   TInfo->SetMessageText("");
  500. }
  501.  
  502. static char * CharArray[] = { "a","b", "c", "d", "e", "f", "g", "h" };
  503. static char * NumArray[] = { "1", "2", "3", "4", "5", "6", "7", "8" };
  504.  
  505. static void
  506. DrawBump(HDC hDC, int x, int y)
  507. {
  508.   int x2 = x + CHARSIZE + 2;
  509.   int y2 = y-- + LINESIZE + 1;
  510.   x -= 2;
  511.  
  512.   HANDLE hOldBrush = SelectObject(hDC, GetStockObject(LTGRAY_BRUSH));
  513.   HANDLE hOldPen = SelectObject(hDC, GetStockObject(WHITE_PEN));
  514.  
  515.   Rectangle(hDC, x, y, x2, y2);
  516.  
  517.   SelectObject(hDC, CreatePen(PS_SOLID, 1, RGB(128, 128, 128)));
  518.   TPoint points[3];
  519.   points[0].x = ++x;
  520.   points[1].y = points[0].y = y2;
  521.   points[2].x = points[1].x = x2;
  522.   points[2].y = ++y;
  523.   Polyline(hDC, points, 3);
  524.  
  525.   SelectObject(hDC, hOldBrush);
  526.   DeleteObject(SelectObject(hDC, hOldPen));
  527. }
  528.  
  529. static void
  530. DrawAlphaNum()
  531. {
  532.   HDC hDC = GetDC(hWndMain);
  533.  
  534.   int xPos = (BORDERSIZE + MYFRAMESIZE)/2 - CHARSIZE/2;
  535.   int yPos = BORDERSIZE + SQUARE_SIZE/2 - LINESIZE/2;
  536.  
  537.   SetBkColor(hDC, RGB(192, 192, 192));
  538.  
  539.   int i;
  540.   for (i = 7; i >= 0; i--) {
  541.     DrawBump(hDC, xPos, yPos);
  542.     if (Turned)
  543.       TextOut(hDC, xPos, yPos, NumArray[7-i], 1);
  544.     else
  545.       TextOut(hDC, xPos, yPos, NumArray[i], 1);
  546.     yPos += SQUARE_SIZE;
  547.   }
  548.  
  549.   xPos = BORDERSIZE + SQUARE_SIZE/2 - CHARSIZE/2;
  550.   yPos = BORDERSIZE + 8*SQUARE_SIZE + 2*MYFRAMESIZE + 1;
  551.  
  552.   for (i = 0; i < 8; i++) {
  553.     DrawBump(hDC, xPos, yPos);
  554.     if (Turned)
  555.       TextOut(hDC, xPos, yPos, CharArray[7-i], 1);
  556.     else
  557.       TextOut(hDC, xPos, yPos, CharArray[i], 1);
  558.     xPos += SQUARE_SIZE;
  559.   }
  560.   ReleaseDC(hWndMain, hDC);
  561. }
  562.  
  563. void
  564. SetClassWindowCursor(HWND hWnd, HCURSOR hCursor)
  565. {
  566. #if defined(BI_PLAT_WIN32)
  567.   SetClassLong(hWnd, GCL_HCURSOR, long(hCursor));
  568. #else
  569.   SetClassWord(hWnd, GCW_HCURSOR, WORD(hCursor));
  570. #endif
  571.   SetCursor(hCursor);
  572. }
  573.